﻿using System;
using System.Collections.Generic;

using DflyingForum.Core.Data.Domain.Entities;
using DflyingForum.Core.Data.Domain.ValueObjects;

namespace DflyingForum.Core.Repositories
{
    public class PostReposity : NHibernateRepository<Posts, int>
    {
        /// <summary>
        /// 热帖算法（简单实现一）
        /// </summary>
        /// <param name="postId">帖子ID</param>
        /// <returns></returns>
        public virtual bool HowToGetHotPost(int postId)
        {
            Posts post = FindBy(postId);
            int rCount = post.ReplyCount;
            int hCount = post.Hits;
            if (rCount >= 40 && hCount >= 100 && (((float)rCount / hCount) >= 0.014))
            {
                return true;
            }

            /*不重复的回复数
            string hql = "select distinct count(r.User) from Posts p inner join p.Replys r where p.ID=:fn";
            int count = Convert.ToInt32(base.FindObjectBy(hql, "fn", postId)[0][0]);
             */

            return false;
        }

        /// <summary>
        /// 是否是精华帖
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public bool IsExcellent(Posts post)
        {
            var postState = post.PostStates;
            foreach (var ps in postState)
            {
                if (ps.Flag == (int)EnumPostState.Excellent)
                    return true;
            }
            return false;
        }
    }
}